package com.woniuxy.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.woniuxy.utils.Utils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 事务型  保证生产者发送信息的原子性：要么全部发送成功 要么全部失败
 */
public class SelectProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接
        Connection connection = Utils.getConnection();
        //对接通道
        Channel channel = connection.createChannel();
        //开启事务
        channel.txSelect();
        //发布到对应的交换机和routinkey
        channel.basicPublish("comfirmExchange","confirm1",null,"事务型：确保消息发送开始".getBytes());
        int a=10/0;
        channel.basicPublish("comfirmExchange","confirm1",null,"事务型：确保消息完成".getBytes());
        System.out.println("发送消息完成");
        //提交事务
        channel.txCommit();
        //关闭资源
        channel.close();
        connection.close();
    }
}
